package com.su.lrhand;

import java.util.Random;

import javax.security.auth.callback.Callback;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.lidroid.xutils.exception.DbException;
import com.su.dolphin.core.AppThread;
import com.su.dolphin.model.db.DpBaseDao;
import com.su.dolphin.view.activity.BaseActivity;

public class MainActivity extends BaseActivity
{
    
    private static final int LEFT = 0;
    private static final int RIGHT = 1;
    
    private TextView mTv_dashboard;
    private Button mBtn_left;
    private Button mBtn_right;
    private int mCurrentDirection;
    private boolean isRun;
    private TextView mTv_result;
    private int mTotalCount, mCorrectCount;
    private TextView mBtn_refresh;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    
    @Override
    public void initView() {
        mTv_dashboard = (TextView) findViewById(R.id.tv_dashboard);
        mBtn_left = (Button) findViewById(R.id.btn_left);
        mBtn_right = (Button) findViewById(R.id.btn_right);
        mTv_result = (TextView) findViewById(R.id.tv_result);
        mBtn_refresh = (TextView) findViewById(R.id.btn_refresh);
        
    }
    
    @Override
    public void setListener() {
        
    }
    
    @Override
    public void onClick(View arg0) {
        switch (arg0.getId()) {
            case R.id.btn_refresh:
                isRun = false;
                mBtn_refresh.setText("刷新");
                AppThread.uiPost("", new Runnable()
                {
                    
                    @Override
                    public void run() {
                        mTotalCount = 0;
                        mCorrectCount = 0;
                        isRun = true;
                        new Thread(mGameRunable).start();
                    }
                }, 100);
                
                break;
            case R.id.btn_left:
                if (mTotalCount == 40) {
                    break;
                }
                if (mCurrentDirection == LEFT) {
                    mTv_dashboard.setTextColor(Color.GREEN);
                    mCorrectCount++;
                }
                else {
                    mTv_dashboard.setTextColor(Color.RED);
                    
                }
                break;
            case R.id.btn_right:
                if (mTotalCount == 40) {
                    break;
                }
                if (mCurrentDirection == LEFT) {
                    mTv_dashboard.setTextColor(Color.RED);
                }
                else {
                    mCorrectCount++;
                    mTv_dashboard.setTextColor(Color.GREEN);
                    
                }
                break;
            default:
                break;
        }
        mTv_result.setText(mCorrectCount + "/" + mTotalCount);
    }
    
    private Runnable mGameRunable = new Runnable()
    {
        
        @Override
        public void run() {
            while (isRun) {
                mTotalCount++;
                if (mTotalCount == 40) {
                    isRun = false;
                    break;
                }
                Random rand = new Random();
                mCurrentDirection = rand.nextInt() % 2;
                mHandler.sendEmptyMessage(mCurrentDirection);
                try {
                    Thread.sleep(1000);
                }
                catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            
        }
    };
    private Handler mHandler = new Handler(new Handler.Callback()
    {
        
        @Override
        public boolean handleMessage(Message msg) {
            mTv_dashboard.setTextColor(Color.BLACK);
            String text = mCurrentDirection == LEFT ? "左" : "右";
            mTv_dashboard.setText(text);
            return false;
        }
    });
    
    @Override
    public void initData() {
        
    }
    
    @Override
    protected void onDestroy() {
        isRun = false;
        super.onDestroy();
    }
    
}
